#!/usr/bin/php -q
<?php
include 'includes/config.php';

$corpname = $corpI->cname;
$dbuser = $snowD->uname;
$dbpwd = $snowD->pass;
$dbname = $snowD->db;
$killboard_host = $corpI->host;
$killboard_path = $corpI->path;

$enable_losses = 0; // not implemented

////////////////////////////////////////////////////////////////////////

if($argc == 2 && $argv[1] == "init") $init = 1; else $init = 0;

////////////////////////////////////////////////////////////////////////

function db_query($q){
	//     on -->   failure     no-match    match
	// return -->   FALSE       TRUE        array

	$ra = array();
	if($ro = mysql_query($q)){
		if(!is_resource($ro)) return 1;
		while($row = mysql_fetch_row($ro)) $ra[] = $row;
		mysql_free_result($ro);
	} else {
		printf("database query failed: %s<br />", mysql_error());
	}
	if(count($ra) == 0) return 0; else return $ra;
}
function db_query_value($q){
	$x = db_query($q);
	if(is_array($x) && count($x) == 1){
		if(count($x[0]) == 1) return $x[0][0];
		return $x[0];
	}
	return 0;
}
function fetch_killboard($type){
	global $killboard_host, $killboard_path, $init, $corpname;

	$query = "corp=$corpname&type=$type".($init ? "&init=1" : "");
	$fp = fsockopen($killboard_host, 80, $errno, $errstr, 30);
	if(!$fp){ echo "($killboard_path) couldn't talk to killboard server: $errstr\n"; return NULL; }
	$header  = "Host: $killboard_host\r\n";
	$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
	$header .= "Content-Length: " . strlen($query) . "\r\n";
	$header .= "Connection: close\r\n\r\n";
	fputs($fp, "POST $killboard_path HTTP/1.0\r\n" . $header . $query);
	$result = array(); $body = 0;
	while(!feof($fp)){
		$line = trim(fgets($fp, 1024));
		if(empty($line)){ $body = 1; continue; }
		if($body) $result[] = explode(" ", $line);
	}
	return $result;
}

////////////////////////////////////////////////////////////////////////

mysql_connect("localhost", $dbuser, $dbpwd)
	or die("could not connect to database: ". mysql_error());
mysql_select_db($dbname);

// KILLS:  kll_id, ind_plt_id, kll_timestamp, sys_eve_id
// LOSSES: kll_id, kll_victim_id, kll_timestamp, sys_eve_id

//CREATE TABLE kills (kill_id integer unsigned NOT NULL, pilot_id integer unsigned NOT NULL,
// system_id integer unsigned NOT NULL, timestamp integer unsigned NOT NULL);
//kill_id and pilot_id are primary keys

//$t = time();

// update kills
$kills = fetch_killboard(0);
if(is_array($kills))
	foreach($kills as $kill){
		if(db_query_value("SELECT pilot_id FROM kills WHERE kill_id = $kill[0] AND pilot_id = $kill[1];")) continue; // kill existsi
		$main=db_query("SELECT altOf FROM alts WHERE charID = $kill[1];");
		if(is_array($main)) $main = $main[0][0]; else $main = $kill[1];
		db_query("INSERT INTO kills SET kill_id = $kill[0], pilot_id = $kill[1], timestamp = $kill[2], system_id = $kill[3], main_id = $main;");
	}

if($enable_losses){ // fetch losses

	// TODO

}

//echo "\n\n".(time()-$t)." seconds\n";

mysql_close();
?>
